
package com.yimixian.app.rest.api;

import android.text.TextUtils;

import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Comment;
import com.yimixian.app.model.Comments;
import com.yimixian.app.model.SearchKeyResult;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.client.Response;

/**
 * Created by songzy on 2015/10/26.
 */
public class CommentGetAPI extends YmxBaseAPI<Comments> {

    public static final String COMMENT_GET = "/v5/commodity/{good_id}/comments";
    public static final String COMMENT_GET_COMMENTS = "comments";

    private int mGoodId;
    private String mOffset;
    private int mSize;

    /**
     * @param callback
     */
    public CommentGetAPI(int goodId, String offset, int size, Callback<Response> callback) {
        super(callback);
        mGoodId = goodId;
        mOffset = offset;
        mSize = size;
    }

    public void commentGet() {
        String token = DataManager.getInstance().get(CacheKeys.TOKEN);
        if (mGoodId == 0) {
            EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_DISSMISS_DIALOG));
            return;
        }
        int goodId = mGoodId;
        String offset = mOffset;
        int size = mSize;
        if(TextUtils.isEmpty(token)) {
            mYmxRestService.commentGet(goodId, offset, size, mCallback);
        } else {
            mYmxRestService.commentGet(token, goodId, offset, size, mCallback);
        }
    }

    @Override
    public String getRelativeURL() {
        return COMMENT_GET;
    }

    @Override
    public Comments parseJson(JSONObject jsonObject) {
        Comments comments = new Comments();
        if (jsonObject != null) {
            JSONArray commentsArray = jsonObject.optJSONArray("comments");
            if (commentsArray != null) {
                comments.comments = new ArrayList<>();
                for (int i = 0; i < commentsArray.length(); i++) {
                    Comment comment = new Comment();
                    JSONObject commentObj = commentsArray.optJSONObject(i);
                    if (commentObj != null) {
                        comment.id = commentObj.optString("id");
                        comment.goodId = commentObj.optInt("good_id");
                        comment.reviewerId = commentObj.optInt("reviewer_id");
                        comment.reviewerName = commentObj.optString("reviewer_name");
                        comment.content = commentObj.optString("content");
                        comment.tasteDesc = commentObj.optString("taste_desc");
                        comment.taste = commentObj.optInt("taste");
                        comment.freshnessDesc = commentObj.optString("freshness_desc");
                        comment.freshness = commentObj.optInt("freshness");
                    }
                    comments.comments.add(comment);
                }
            }
            comments.latestId = jsonObject.optString("latest_id");
            comments.count = jsonObject.optInt("count");
        }
        return comments;
    }
}
